package io.renren.modules.back.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.back.entity.ShopEntity;
import io.renren.modules.back.service.ShopService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商铺接口
 *
 * @author zzh
 * @email 862583291@qq.com
 * @date 2018年12月10日 上午10:40:10
 */
@RestController
@RequestMapping("/back/shop")
public class ShopController {
    @Autowired
    private ShopService shopService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("shop:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ShopEntity> shopList = shopService.queryList(query);
        int total = shopService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(shopList, total, query.getLimit(), query.getCurrPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("shop:info")
    public R info(@PathVariable("id") Long id) {
        ShopEntity shop = shopService.getById(id);
        return R.ok().put("shop", shop);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("shop:save")
    public R save(@RequestBody ShopEntity shop) {
        ValidatorUtils.validateEntity(shop, AddGroup.class);
        shop.setStatus(1);
        shop.setCreateTime(new Date());
        shopService.save(shop);
        return R.ok("执行成功");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("shop:update")
    public R update(@RequestBody ShopEntity shop) {
        ValidatorUtils.validateEntity(shop, UpdateGroup.class);
        shopService.updateById(shop);
        return R.ok("执行成功");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("shop:delete")
    public R delete(@RequestBody Long[] ids) {
        shopService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
}
